<?php

class User extends Model {
	
	function User()
	{
		parent::Model();
	}

	function register($array)
	{
		$this->db->insert('User', $array);
		return true;
	}

	function rate_authenticate($username, $subject)
	{
		$this->db->where('Username', $username);
		$this->db->where('Subject', $subject);
		$this->db->from('Rating');
		$query = $this->db->get();
		return $query->num_rows();
	}

	function reg_authenticate_username($username)
	{
		$this->db->from('User');
		$this->db->where('Username', $username);
		$query = $this->db->get();
		return $query->num_rows();
	}

	function reg_authenticate_email($email)
	{
		$this->db->from('User');
		$this->db->where('Email', $email);
		$query = $this->db->get();
		return $query->num_rows();
	}

	function change_authenticate_email($username, $email)
	{
		$this->db->from('User');
		$this->db->where('Email', $email);
		$this->db->where('Username', $username);
		$query = $this->db->get();
		return $query->num_rows();
	}

	function update_email($username, $email) {
		$this->db->where('Username',$username);
		$this->db->update('User', array('Email'=>$email));
		return true;
	}

	function change_authenticate_password($username, $password)
	{
		$this->db->from('User');
		$this->db->where('Password', $password);
		$this->db->where('Username', $username);
		$query = $this->db->get();
		return $query->num_rows();
	}

	function update_password($username, $password) {
		$this->db->where('Username',$username);
		$this->db->update('User', array('Password'=>$password));
		return true;
	}

	function login_authenticate($username, $password)
	{
		$this->db->from('User');
		$this->db->where('Username',$username);
		$this->db->where('Password', $password);
		$query = $this->db->get();
		return $query->row_array();
	}


	function update_profile($username, $array)
	{
		$this->db->where('Username',$username);
		$this->db->update('User', $array);
		return true;
	}

	function update($date, $username)
	{
		$this->db->where('Username',$username);
		$this->db->update('User', array('LastLogOut'=>$date['LastLogOut']));
		return true;
	}


	function getLastLogOut($username) {
		//$this->db->from('User');
		//$this->db->where('Username', $username);
		//$query = $this->db->get();
		$query = $this->db->query('SELECT DATE_FORMAT(LastLogOut, "%W, %m.%e.%y @ %h:%i %p") as `Last` FROM User where Username = ?', $username);
		return $query;
	}

	function delete($username)
	{
		$this->db->delete('User', array('Username'=>$username));
		return TRUE;
	}
}

?>
